package com.lianboke.link.ad.activity;

import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.lianboke.link.ad.R;
import com.lianboke.link.ad.VolleyUtils.HttpCallBack;
import com.lianboke.link.ad.VolleyUtils.HttpUtil;
import com.lianboke.link.ad.ad.YOUMiADUitil;
import com.lianboke.link.ad.app.MyApplication;
import com.lianboke.link.ad.beans.JiFenBean;
import com.lianboke.link.ad.beans.UserAccountModel;
import com.lianboke.link.ad.common.CommonParams;
import com.lianboke.link.ad.utils.DeviceUtils;
import com.lianboke.link.ad.utils.ToastUtil;

import java.util.HashMap;

import butterknife.BindView;

/**
 * 有米广告区域
 */
public class YouMiAdActivity extends BaseActivity {

    @BindView(R.id.tv_user_youmi_jifen_value)
    public TextView tv_user_youmi_jifen_value;

    @BindView(R.id.btn_submit)
    public Button btn_submit;
    @BindView(R.id.tv_user_youmi_jifen_rule)
    public TextView tv_user_youmi_jifen_rule;

    @BindView(R.id.tv_youmi_jifen)
    public TextView tv_youmi_jifen;

    @BindView(R.id.loading_progress)
    public ProgressBar loading_progress;

    private UserAccountModel userAccountModel;


    @Override
    public int getContentViewId() {
        return R.layout.activity_you_mi_ad;
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }

    @Override
    public void initActivityParams(Bundle savedInstanceState) {
        setTitleBackShow(true);
        setTitleContent("积分兑换区一");
        //展示有米广告
        tv_youmi_jifen.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                YOUMiADUitil.fullScreenYOUMiOffersWall(actContext);
            }
        });
        //兑换
        btn_submit.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                exChangeJinFen();
            }
        });
        //规则
        tv_user_youmi_jifen_rule.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

            }
        });
        tv_user_youmi_jifen_rule.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG );
        getUserAccountInfo();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getUserAccountInfo();
    }

    /**
     * 显示加载进度
     */
    private void showLoadingProgress(){
        if(loading_progress!=null && loading_progress.getVisibility()!= View.VISIBLE){
            loading_progress.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏加载进度
     */
    private void hideLoadingProgress(){
        if(loading_progress!=null && loading_progress.getVisibility()!=View.GONE){
            loading_progress.setVisibility(View.GONE);
        }
    }

    /**
     * 获取用户信息
     */
    public void getUserAccountInfo(){
        showLoadingProgress();
        HashMap<String,String> params = new HashMap<String,String>();
        params.put("deviceid", DeviceUtils.getImei(appContext));
        params.put("version",DeviceUtils.getVersionName(actContext));
        params.put("nettypename",DeviceUtils.getDeviceAc());
        params.put("city", MyApplication.strCity);
        HttpUtil.getInstance().request_get(CommonParams.URL_WEB_GET_USER_FIND_ACCOUNT, params, new HttpCallBack<UserAccountModel>() {
            @Override
            public void onSuccess(UserAccountModel data) {
                userAccountModel = data;
                hideLoadingProgress();
                showUserAccountInfo();
            }

            @Override
            public void onFail(String msg) {
                hideLoadingProgress();
            }
        });
    }
    /**
     * 显示用户嘻嘻
     */
    private void  showUserAccountInfo(){
        if(userAccountModel!=null){
            tv_user_youmi_jifen_value.setText(userAccountModel.getMiyoujifen());
        }
    }

    /**
     * 兑换积分
     */
    private void exChangeJinFen(){
        showLoadingProgress();
        HashMap<String,String> params = new HashMap<String,String>();
        params.put("deviceid", DeviceUtils.getImei(appContext));
        params.put("version",DeviceUtils.getVersionName(actContext));
        params.put("nettypename",DeviceUtils.getDeviceAc());
        params.put("city", MyApplication.strCity);
        params.put("channel","1");
        HttpUtil.getInstance().request_get(CommonParams.URL_WEB_JIFEN_EXCHANGE, params, new HttpCallBack<JiFenBean>() {
            @Override
            public void onSuccess(JiFenBean data) {
                hideLoadingProgress();
                if(data!=null && data.getC()==200){
                    //兑换成功
                    getUserAccountInfo();
                    ToastUtil.showShort(actContext,"成功兑换积分！");
                }else if(data!=null){
                    //兑换失败
                    ToastUtil.showShort(actContext,data.getM());
                }else {
                    ToastUtil.showShort(actContext,data.getM());
                }
            }

            @Override
            public void onFail(String msg) {
                hideLoadingProgress();
                ToastUtil.showShort(actContext,msg);
            }
        });
    }
}
